package com.cat.dataStructure03;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/minimum-adjacent-swaps-to-alternate-parity/
 * @create 2025/11/11 10:26
 * @since JDK17
 */

class Solution42 {
    int calc(int start, List<Integer> p, int n) {
        if ((n - start + 1) / 2 != p.size()) {
            return Integer.MAX_VALUE;
        }
        int ans = 0;
        for (int i = 0; i < p.size(); i++) {
            ans += Math.abs(i * 2 + start - p.get(i));
        }
        return ans;
    }

    public int minSwaps(int[] nums) {
        int ans = 0, n = nums.length;
        List<Integer> p = new ArrayList<>();
        for (int i = 0; i < n; i++) {   // 记录所有的奇数位置
            if (nums[i] % 2 != 0) {
                p.add(i);
            }
        }
        ans = Math.min(calc(0, p, n), calc(1, p, n));
        return ans == Integer.MAX_VALUE ? -1 : ans;
    }
}
